package panel.viewSpots.navigationNew
{
	import assists.Lang;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.GeolocationEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.sensors.Geolocation;
	import flash.text.TextField;
	
	import inerface.INavigatonPosChoose;
	
	import main.ClientConstants;
	
	import panel.viewSpots.StaticSlide;
	import panel.viewSpots.navigation.BtnClouree;
	
	public class NavigationPosChoosePanel extends Sprite implements INavigatonPosChoose
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var _tipTxt:TextField = new TextField();
		
		private var _btnContainer:Sprite = new Sprite();
		private var _geolocation:Geolocation;
		private var _posInfo:Object = {};
		private var _dataList:Array = [];
		private var _dataDisList:Array = [];
		private var _onOpenDetail:Function;
		private var _onOpenNavigation:Function;
		public function NavigationPosChoosePanel()
		{
			super();
		}
		
		public function init(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			
			_tipTxt.y = ClientConstants.HOME_UP_PER * _stage.stageHeight;
			_tipTxt.mouseEnabled = false;
			_tipTxt.autoSize = "left";
//			this.addChild(_tipTxt);
		    this.addChild(_btnContainer);
		}
		
		public function show(dataList:Array):void
		{
			_dataList = dataList;
//			onGeolocUpdate();
//			generateBox();
			getCurrentPos();
		}
		
		private function getCurrentPos():void
		{
			if(Geolocation.isSupported)
			{
				if(!_geolocation) _geolocation = new Geolocation();
				if(_geolocation.muted == false)
				{
					_geolocation.addEventListener(GeolocationEvent.UPDATE,onGeolocUpdate);	
				}
			}
		}
		
		protected function onGeolocUpdate(event:GeolocationEvent = null):void
		{
			_posInfo.latitude = event.latitude//37.39570//event.latitude;
			_posInfo.longitude = event.longitude//122.70520//event.longitude;
			trace(_posInfo.latitude + "___" + _posInfo.longitude);
//			_geolocation.removeEventListener(GeolocationEvent.UPDATE,onGeolocUpdate);	
			
//			TextFieldUtils.setHtmlText(_tipTxt,HtmlText.format("您未在任何景点附近！",0xffffff));
			_btnContainer.y = _tipTxt.y + _tipTxt.height;
			
			for(var i:int=0;i<_dataList.length;i++)
			{
			seekPosSpot(_dataList[i]);
			}
			_dataDisList.sortOn("dis",Array.NUMERIC);
			
			generateBox();
		}
		
		private function generateBox():void
		{
			var boxX:int = _stage.stageWidth * 0.1;
			var boxW:int = _stage.stageWidth * 0.8;
			
			for(var i:int=0;i<_dataDisList.length;i++)
			{
			 var box:BtnClouree = new BtnClouree();
			 box.data = _dataDisList[i];
			 box.x = boxX;
			 box.autoSize(boxW);
			 if(_dataDisList[i].dis < 3)
			 {
				 box.label(_dataDisList[i].data.name + "\n（您当前所在）",22); 
			 }else
			 {
			 box.label(_dataDisList[i].data.name + "\n" + _dataDisList[i].dis + "公里",22);
			 }
			 box.y = i * box.height * 1.2;
			 _btnContainer.addChild(box);
			 box.addEventListener(MouseEvent.CLICK,clickBoxHandler);
			}
//			_btnContainer.y = _btnContainer.y + 200;
//			_btnContainer.graphics.beginFill(0,0.6);
//			_btnContainer.graphics.drawRect(_btnContainer.x,_btnContainer.y,_btnContainer.width,_btnContainer.height);
//			_btnContainer.graphics.endFill();
//			StaticSlide.beginSlide(_btnContainer,_stage,new Rectangle(0,_btnContainer.y,0,-_stage.stageHeight +  _stage.stageHeight * (ClientConstants.HOME_DOWN_PER + ClientConstants.HOME_UP_PER)));
//trace(_stage.stageHeight + "------------" + _btnContainer.height);
			StaticSlide.beginSlide(_btnContainer,_stage,new Rectangle(0,_btnContainer.y,0,-_btnContainer.height + _stage.stageHeight * (ClientConstants.HOME_DOWN_PER + ClientConstants.HOME_UP_PER)));
		}
		
		protected function clickBoxHandler(event:MouseEvent):void
		{
			var box:BtnClouree = event.currentTarget as BtnClouree;
			if(box.data.dis < 3)
			{
				_onOpenNavigation(box.data.data);
			}else
			{
				_onOpenDetail(box.data.data,ClientConstants.DETAIL_VIEWSPOT);
			}
		}
		
		private function seekPosSpot(data:Object):void
		{
			var disX:Number = Math.abs(data.location[0] - _posInfo.latitude) * 110.94;
			var disY:Number = Math.abs(data.location[1] - _posInfo.longitude) * 85.276;
			var info:Object = {}; 
			info.dis = commuteDis(disX,disY);
			info.data = data;
			_dataDisList.push(info);
		}
		
		private function commuteDis(xDis:Number,yDis:Number):Number
		{
			return Number(Math.sqrt(Math.abs(Math.pow(xDis,2) - Math.pow(yDis,2))).toFixed(1));
		}
		
		public function set openDetail(value:Function):void
		{
			_onOpenDetail = value;
		}
		
		public function set openNavigation(value:Function):void
		{
			_onOpenNavigation = value;
		}
		
		public function clear():void
		{
		}
	}
}